package io.zcoder.innoway.adapters;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import io.zcoder.innoway.R;
import io.zcoder.innoway.utils.Utils;
import io.zcoder.innoway.models.EntryTime;
import io.zcoder.innoway.widgets.TextViewCustom;


public class HistoryEntryTimeByMonthAdapter extends RecyclerView.Adapter<HistoryEntryTimeByMonthAdapter.EntryPlaceHolder> {

    private List<EntryTime> mEntryTimes;
    private String mUnit;
    private boolean mModeDelete;

    public HistoryEntryTimeByMonthAdapter(List<EntryTime> entryTimes, String unit) {
        mEntryTimes = entryTimes;
        mUnit = unit;
    }

    public class EntryPlaceHolder extends RecyclerView.ViewHolder {
        TextViewCustom mTime, mValue;
        LinearLayout mLayoutDelete, mLayoutItem;
        ImageView mIconDelete;

        public EntryPlaceHolder(View itemView) {
            super(itemView);
            mTime = (TextViewCustom) itemView.findViewById(R.id.time_text);
            mValue = (TextViewCustom) itemView.findViewById(R.id.value);
            mLayoutDelete = (LinearLayout) itemView.findViewById(R.id.layout_delete);
            mLayoutItem = (LinearLayout) itemView.findViewById(R.id.layout_item);
            mIconDelete = (ImageView) itemView.findViewById(R.id.ic_close);

        }
    }


    @Override
    public EntryPlaceHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_history_trend_by_month, parent, false);

        return new EntryPlaceHolder(itemView);
    }

    @Override
    public void onBindViewHolder(EntryPlaceHolder holder, int position) {
        final EntryTime entryTime = mEntryTimes.get(position);
        holder.mTime.setText(Utils.convertToString(Utils.convertToDate(entryTime.getTime(),
                Utils.DATE_FORMAT_FULL), Utils.DATE_FORMAT_MMM_DD_YYYY_HH_MM_AA));
        holder.mValue.setText(entryTime.getValueWithUnit(mUnit));
        if (mModeDelete) {
            holder.mLayoutDelete.setVisibility(View.VISIBLE);
            if (entryTime.isChecked()) {
                holder.mLayoutDelete.setBackgroundResource(R.drawable.circle_fill_red);
                holder.mIconDelete.setVisibility(View.VISIBLE);
            } else {
                holder.mLayoutDelete.setBackgroundResource(R.drawable.circle_border_gray);
                holder.mIconDelete.setVisibility(View.GONE);
            }
        } else {
            holder.mLayoutDelete.setVisibility(View.GONE);
        }
        holder.mLayoutItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mModeDelete) {
                    entryTime.setChecked(!entryTime.isChecked());
                    notifyDataSetChanged();
                }
            }
        });
    }


    @Override
    public int getItemCount() {
        return mEntryTimes == null ? 0 : mEntryTimes.size();
    }

    public void setModeDelete(boolean b) {
        mModeDelete = b;
        notifyDataSetChanged();
    }

    public List<Integer> getItemDelete() {
        List<Integer> list = new ArrayList<>();
        for (EntryTime entryTime : mEntryTimes) {
            if (entryTime.isChecked()) {
                list.add(entryTime.getId());
            }
        }
        return list;
    }

}
